package ops.view.tables;

import java.util.LinkedHashSet;

import javax.swing.JButton;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.TableColumnModel;
import javax.swing.table.TableModel;

public class TableMaker {

	private LinkedHashSet<Column> columns;
	private JTable table = null;
	private JButton[] buttons;
	
	public TableMaker() {
		columns = new LinkedHashSet<Column>();
	}
	
	/**
	 * Add a column to the table
	 * @param column
	 */
	public void addColumn(Column column) {
		columns.add(column);
	}
	
	/**
	 * Add data to the table
	 * @param data
	 */
	public void addRow(Object... data) {
		((CustomTableModel) table.getModel()).addRow(data);
	}
	
	/**
	 * @param row
	 * @param column
	 * @return the same as {@link TableModel}.getValueAt()
	 */
	public Object getValueAt(int row, int column) {
		return ((CustomTableModel) table.getModel()).getValueAt(row, column);
	}
	
	public int getSeletedRow() {
		return table.getSelectedRow();
	}
	
	/**
	 * @return the Swing component representing the table
	 */
	public JTable getTable() {
		if (table == null) {
			prepareTable();
		}
		
		return table;
	}
	
	/**
	 * Clears the table's data.
	 */
	public void clearTable() {
		CustomTableModel model = (CustomTableModel) table.getModel();
		while (model.getRowCount() != 0) {
			model.removeRow(0);
		}
	}

	/**
	 * Adds a SelectionListener to the table.
	 * @param l
	 */
	public void addSelectionListener(ListSelectionListener l) {
		getTable().getSelectionModel().addListSelectionListener(l);
	}
	
	/**
	 * Simply clears the selection of the table.
	 */
	public void clearSelection() {
		getTable().clearSelection();
	}
	
	/**
	 * Set which buttons should be enabled/disabled if a selection/de-selection happens in the table.
	 * @param buttons
	 */
	public void setControlButtons(JButton... buttons) {
		this.buttons = buttons;
		
		disableButtons();
	}
	
	private void prepareTable() {
		CustomTableModel tableModel = new CustomTableModel(columns);
		table = new JTable(tableModel);
		table.setAutoCreateRowSorter(true);
		table.setAutoResizeMode(JTable.AUTO_RESIZE_SUBSEQUENT_COLUMNS);
		table.setModel(tableModel);
		table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);	
		
		addSelectionListener(new SelectionListener());
		
		TableColumnModel columnModel = table.getColumnModel();
		int i = 0;
		for (Column column : columns) {
			
			if (column.getSize() != Integer.MIN_VALUE) {
				columnModel.getColumn(i).setPreferredWidth(column.getSize());
			}			
			
			i++;
		}
	}
	
	private class SelectionListener implements ListSelectionListener {
		public void valueChanged(ListSelectionEvent e) {
			if (e.getValueIsAdjusting()) {
				return;
			}
			
			ListSelectionModel lsm = (ListSelectionModel) e.getSource();
			
			if (lsm.isSelectionEmpty()) {
				disableButtons();
			} else {
				enableButtons();
			}
		}
	}	

	private void enableButtons() {	
		if (buttons != null) {
			for (JButton button : buttons) {
				button.setEnabled(true);
			}
		}
	}

	private void disableButtons() {
		if (buttons != null) {
			for (JButton button : buttons) {
				button.setEnabled(false);
			}
		}
	}
	
}
